package com.ytjj.qmyx.supplychain.service;

import com.ytjj.qmyx.supplychain.common.model.response.IndexDataResponse;
import com.ytjj.qmyx.supplychain.common.model.response.IndexRankResponse;
import com.ytjj.qmyx.supplychain.mapper.IndexMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBanker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 
 * 
 * @Version: 1.0
 */
@Service
public class IndexService {
    @Autowired
    private BankerService bankerService;
    @Resource
    private IndexMapper indexMapper;

    public IndexDataResponse getIndexData() {
        // 获取此前登录的供应商信息
        YxBanker userInfo = bankerService.getUserInfo();
        Integer bankerId = userInfo.getId();

        IndexDataResponse indexData = new IndexDataResponse();

        Integer allOrderNum = indexMapper.getAllOrderNum(bankerId);
        BigDecimal totalMoney = indexMapper.getTotalMoney(bankerId);
        if(totalMoney == null) {
            totalMoney = BigDecimal.ZERO;
        }
        Integer waitDeliverGood = indexMapper.getWaitDeliverGood(bankerId);
        Integer allGood = indexMapper.getAllGood(bankerId);
        BigDecimal totalRevenue = indexMapper.getTotalRevenue(bankerId);
        if(totalRevenue == null) {
            totalRevenue = BigDecimal.ZERO;
        }
        Integer upGood = indexMapper.getUpGood(bankerId);
        Integer downGood = indexMapper.getDownGood(bankerId);
        Integer examingGood = indexMapper.getExamingGood(bankerId);

        indexData.setAllOrderNum(allOrderNum)
                .setTotalMoney(totalMoney)
                .setWaitDeliverGood(waitDeliverGood)
                .setAllGood(allGood)
                .setTotalRevenue(totalRevenue)
                .setUpGood(upGood)
                .setDownGood(downGood)
                .setExamingGood(examingGood);

        return indexData;
    }

    public List<IndexRankResponse> getIndexRank(String dateType) {
        // 获取此前登录的供应商信息
        YxBanker userInfo = bankerService.getUserInfo();
        Integer bankerId = userInfo.getId();

        if("".equals(dateType) || null == dateType) {
            dateType = "DAY";
        }
        List<IndexRankResponse> indexRank = indexMapper.getIndexRankResponse(bankerId, dateType);

        try {
            if(!CollectionUtils.isEmpty(indexRank)) {
                for(IndexRankResponse rank : indexRank) {
                    // 获取收益 1.拿到商品成本价 2.计算收益=成本价 * 成交量
                    Integer orderId = Integer.valueOf(rank.getOrderNo().substring(4, rank.getOrderNo().length()));
                    BigDecimal costPrice = indexMapper.getCostPrice(orderId);
                    BigDecimal profit = BigDecimal.ZERO;
                    if(rank.getTurnover() != null && rank.getTurnover() > 0 && costPrice != null) {
                        profit = new BigDecimal(rank.getTurnover()).multiply(costPrice);
                    }
                    rank.setProfit(profit);
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return indexRank;
    }
}
